package com.example.gmallproduct.service.impl;

import com.example.gmallproduct.entity.SpuInfoEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.utils.PageUtils;
import com.common.utils.Query;

import com.example.gmallproduct.dao.SkuInfoDao;
import com.example.gmallproduct.entity.SkuInfoEntity;
import com.example.gmallproduct.service.SkuInfoService;


@Service("skuInfoService")
public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoDao, SkuInfoEntity> implements SkuInfoService {

    @Autowired
    private SkuInfoDao skuInfoDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                new QueryWrapper<SkuInfoEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryListByKey(Map<String, Object> params) {
        String key = (String) params.get("key");
        long brandId = Long.parseLong((String) params.get("brandId"));
        long catelogId = Long.parseLong((String) params.get("catelogId"));
        long min = Long.parseLong((String) params.get("min"));
        long max = Long.parseLong((String) params.get("max"));

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<>();
        if (brandId != 0) {
            wrapper.eq("brand_id", brandId);
        }
        if (catelogId != 0) {
            wrapper.eq("catalog_id", catelogId);
        }
        if (!((min == 0 && max == 0) || (min > max))) {
            wrapper.between("price", min, max);
        } else if (min == 0 && max == 0) {
            //默认查所有,,0-0
            if (key != null && key.length() > 0) {
                wrapper.like("sku_name", key).or().like("sku_desc", key);
            }
        }

        IPage<SkuInfoEntity> page = this.page(
                new Query<SkuInfoEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public List<SkuInfoEntity> queryBySpuId(Long spuId) {

        QueryWrapper<SkuInfoEntity> wrapper = new QueryWrapper<SkuInfoEntity>().eq("spu_id", spuId);
        List<SkuInfoEntity> list = skuInfoDao.selectList(wrapper);

        return list;
    }

}